package com.itheima.excel.controller;

import com.itheima.domain.Student;
import com.itheima.excel.service.ExcelServiceImpl;
import com.itheima.excel.util.ExportExcel;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.FileWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

/*
导出
 ExportExcel
 */
public class Export {
    public static void exportExcel(String usename) throws Exception {

        long start = System.currentTimeMillis();
        //创建 excel
        HSSFWorkbook excel = new HSSFWorkbook();
        //创建页签
        HSSFSheet sheet1 = excel.createSheet("页签1");
        HSSFSheet sheet2 = excel.createSheet("页签2");
        //标题
        String s1 = "第一个标题";
        String s2 = "第二个标题";
        //表头  Stringg[]
        String[] strs1 = {"学号", "姓名", "性别", "年龄", "生日", "手机号", "组号"};
        String[] strs2 = {"学号", "姓名", "性别", "年龄", "生日", "手机号", "组号"};
        //数据  ArrayList<Object[]>
        ExcelServiceImpl list1 = new ExcelServiceImpl();
        ArrayList<Student> list = list1.getlist();
        //ArrayList<Object[]> list2 = list1.getlist();
        ArrayList<Object[]> list3 = new ArrayList<>();
        list.forEach(s -> list3.add(new String[]{s.getSid(), s.getName(), s.getSex(), s.getAge() + "", s.getBirthday().toString(), s.getPhone(), s.getGroup() + ""}));
        ArrayList<Object[]> list4 = new ArrayList<>();
        list.forEach(s -> list4.add(new String[]{s.getSid(), s.getName(), s.getSex(), s.getAge() + "", s.getBirthday().toString(), s.getPhone(), s.getGroup() + ""}));

                /*
                ArrayList<Object[]> list1 = new ArrayList<>();

                list1.add(new String[]{"0001","kunkun1","261","19961","女1"});
                list1.add(new String[]{"0002","kunkun1","262","19962","女2"});
                list1.add(new String[]{"0003","kunkun1","263","19963","女3"});
                list1.add(new String[]{"0004","kunkun1","264","19964","女4"});
                list1.add(new String[]{"0005","kunkun1","265","19965","女5"});

                list2.add(new String[]{"0001","签签1","261","19961","100"});

                list2.add(new String[]{"0002","签签2","262","19962","200"});
                list2.add(new String[]{"0003","签签3","263","19963","3000"});
                list2.add(new String[]{"0004","签签4","264","19964","4000"});
                list2.add(new String[]{"0005","签签5","265","19965","5000"});

                 */

        ExportExcel export1 = new ExportExcel(s1, strs1, list3);
        export1.export(excel, sheet1, 0);
        ExportExcel export2 = new ExportExcel(s2, strs2, list4);
        export2.export(excel, sheet2, 1);
        long end = System.currentTimeMillis();
        System.out.println("导出excel的毫秒数为" + (end - start));
//
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        LocalDateTime time = LocalDateTime.now();
        String data = time.format(pattern);
        String s = "用户" + usename + "于" + data + "导出excel表格";
        FileWriter fw = new FileWriter("system\\message\\time\\daoTime.txt", true);
        fw.write(s);
        fw.write("r\n");
        fw.close();
    }
}

